#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "../../../../mac/mac.h"

static buf_t *g_buf = NULL;
static int8 arr[128] = {0,};

int8 tx_init()
{
    int8 i, r;

    mac_info_init();
    buf_init();
    i = mac_indir_init();
    r = mac_retry_init();

    if (i == MAC_ERR || r == MAC_ERR) {
        return MAC_ERR;
    } else {
        return MAC_OK;
    }
}

int8 handle_tx16(uint8 *str, bool retry, bool indir)
{
    static uint8 handle = 0;
    static uint16 addr_value = 280;
    static uint8 dsn = 0;
    addr_t addr;
    int8 r;

    addr.mode = ADDR_16_M;
    addr.a.addr16 = addr_value;

    if (g_buf != NULL) {
        buf_free(g_buf);
    }
    g_buf = buf_alloc(false);
    g_buf->length = strlen(str)+1;
    memcpy(g_buf->ptr, str, g_buf->length);
    MAC_DEBUG("handle_tx16: the frame that will be transmitted is: '%s'.\n", g_buf->ptr);

    r = mac_handle_tx(g_buf, &addr, retry, indir, handle, dsn);
    //handle++;
    //addr_value++;
    //dsn++;

    return r;
}

int8 handle_tx64(uint8 *str, bool retry, bool indir)
{
    static uint8 handle = 0;
    static uint64 addr_value = 4800;
    static uint8 dsn = 0;
    buf_t buf;
    addr_t addr;
    uint8 r;

    if (g_buf != NULL) {
        buf_free(g_buf);
    }
    addr.mode = ADDR_64_M;
    addr.a.addr64 = addr_value;

    g_buf = buf_alloc(false);
    g_buf->length = strlen(str)+1;
    memcpy(g_buf->ptr, str, g_buf->length);

    r = mac_handle_tx(g_buf, &addr, retry, indir, handle, dsn);

    //handle++;
    //addr_value++;
    //dsn++;

    return r;
}

/*
int8 csmaca_tx(buf_t *buf, bool indir, uint8 handle)
{
    return mac_csmaca_tx(buf_t *buf, bool indir, uint8 handle);
}
*/


/* as the indir transmission buf the data in the indir buffer, so when retrieving
   the data, we need to got the data from the indir list and delete it */
int8 *get_frame_16(uint8 indir)
{
    MAC_DEBUG("get_frame_16: in it.\n");
    MAC_DEBUG("get_frame_16: indir is %d.\n", indir);
    if (indir == 0) {
        int8 *c = get_tx_frm();
        buf_free(g_buf);
        return c;
    } else {
        buf_t *buf;
        mac_indir_t *i;
        addr_t addr;
        addr.mode = ADDR_16_M;
        addr.a.addr16 = 280;
        MAC_DEBUG("get_frame_16: got here.\n");
        i = mac_indir_find(&addr);
        if (i != NULL) {
            buf = i->buf;
        }
        MAC_DEBUG("get_frame_16: and here.\n");
        if (buf != NULL) {
            MAC_DEBUG("get_frame_16: buf->ptr is '%s'.\n", buf->ptr);
            memset(arr, 0, 128);
            memcpy(arr, buf->ptr, buf->length);
            buf_free(buf);
            mac_indir_free(0);
            g_buf = NULL;
            return arr;
        }
    }
}

int8 *get_frame_64(uint8 indir)
{
    MAC_DEBUG("get_frame_64: in it.\n");
    MAC_DEBUG("get_frame_64: indir is %d.\n", indir);
    if (indir == 0) {
        int8 *c = get_tx_frm();
        buf_free(g_buf);
        return c;
    } else {
        buf_t *buf;
        mac_indir_t *i;
        addr_t addr;
        addr.mode = ADDR_64_M;
        addr.a.addr64 = 4800;
        MAC_DEBUG("get_frame_64: got here.\n");
        i = mac_indir_find(&addr);
        if (i != NULL) {
            buf = i->buf;
        }
        MAC_DEBUG("get_frame_64: and here.\n");
        if (buf != NULL) {
            memset(arr, 0, 128);
            memcpy(arr, buf->ptr, buf->length);
            buf_free(buf);
            mac_indir_free(0);
            g_buf = NULL;
            return arr;
        }
    }
}
 

